﻿#include <stdio.h>
#include "FFmpegVideo.h"

#pragma warning(disable:4996)
extern "C"
{
#include "SDL.h"
}
#pragma execution_character_set("utf-8")
//Refresh Event
#define SFM_REFRESH_EVENT  (SDL_USEREVENT + 1)

#define SFM_BREAK_EVENT  (SDL_USEREVENT + 2)

int thread_exit = 0;
int thread_pause = 0;
long long getCurtimeMsec() {
	SYSTEMTIME sys;
	GetLocalTime(&sys);
	long long curtime = time(NULL);     //总秒数 
	return (long long)curtime * (long long)1000 + (long long)(sys.wMilliseconds);
}
int sfp_refresh_thread(void* opaque) {
	thread_exit = 0;
	thread_pause = 0;

	while (!thread_exit) {

		if (!thread_pause) {
			SDL_Event event;
			event.type = SFM_REFRESH_EVENT;
			SDL_PushEvent(&event);
			
		}
		
		SDL_Delay(40);
	}
	thread_exit = 0;
	thread_pause = 0;
	//Break
	SDL_Event event;
	event.type = SFM_BREAK_EVENT;
	SDL_PushEvent(&event);

	return 0;
}
//启动超时推送线程
static void GetStreamThread(PVOID param) {
	FFmpegVideo* vv = (FFmpegVideo*)param;
	vv->Run();
};
//创建一个拉流线程，并启动它
FFmpegVideo* StartOne( char* filepath, SDL_Renderer* sdlRenderer, int x0, int y0, int w0, int h0) {
	FFmpegVideo* V1=new FFmpegVideo(filepath, sdlRenderer,x0,y0,w0,h0);
	V1->Init();
	HANDLE h1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GetStreamThread, (PVOID)V1, 1, 0); //创建子线程
	ResumeThread(h1);  //启动子线程
	return V1;
}
char* wcharToChar(const wchar_t* s)
{
	size_t len = wcslen(s) + 1;
	size_t converted = 0;
	char* CStr;
	CStr = (char*)malloc(len * sizeof(char));
	wcstombs_s(&converted, CStr, len, s, _TRUNCATE);
	return CStr;
}
int main(int argc, char* argv[])
{
	HWND hwndDOS = GetForegroundWindow();
	ShowWindow(hwndDOS, SW_HIDE);
	//------------SDL----------------
	SDL_Window* screen;
	SDL_Renderer* sdlRenderer;

	SDL_Thread* video_tid;
	SDL_Event event;

	//rtsp://admin:admin123@192.168.1.11:554/Streaming/channels/1
	//char filepath[]="bigbuckbunny_480x272.h265";
	//char filepath[] = "udp://127.0.0.1:6666";
	char* title = new char[200]{ "C++ SDL 多路视频播放器 支持大小调整" };
	char* filepath = new char[200]{"rtsp://admin:admin123@192.168.1.11:554/Streaming/channels/1"};

	//支持参数启动，比如SDL.exe -i rtsp://admin:admin123@192.168.1.11:554/Streaming/channels/1
	for (int count = 1; count < argc; ++count) {
		if (std::string(argv[count]) == "-i" && count + 1 < argc) {
			memset(filepath, 0, 200);
			memcpy_s(filepath, 200, argv[count + 1], strlen(argv[count + 1]));
		}
	}


	
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
		printf("Could not initialize SDL - %s\n", SDL_GetError());
		return -1;
	}


	screen = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
		1280, 720, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);//

	if (!screen) {
		printf("SDL: could not create window - exiting:%s\n", SDL_GetError());
		return -1;
	}
	sdlRenderer = SDL_CreateRenderer(screen, -1, 0);
	video_tid = SDL_CreateThread(sfp_refresh_thread, NULL, NULL);

	int width, height;
	SDL_GetWindowSize(screen, &width, &height);
	//创建4个播放源，并分别指定它们的位置
	FFmpegVideo* V1 = StartOne(filepath, sdlRenderer,0,0,int(width*0.5), int(height * 0.5));
	FFmpegVideo* V2 = StartOne(filepath, sdlRenderer, int(width * 0.5), 0, int(width * 0.5), int(height * 0.5));
	FFmpegVideo* V3 = StartOne(filepath, sdlRenderer, 0, int(height * 0.5), int(width * 0.5), int(height * 0.5));
	FFmpegVideo* V4 = StartOne(filepath, sdlRenderer, int(width * 0.5), int(height * 0.5), int(width * 0.5), int(height * 0.5));



	for (;;) {
		//Wait
		SDL_WaitEvent(&event);
		if (event.type == SFM_REFRESH_EVENT) {

			//循环播放每一帧
			V1->Play();
			V2->Play();
			V3->Play();
			V4->Play();
			SDL_RenderClear(sdlRenderer);
			V1->RenderCopy();
			V2->RenderCopy();
			V3->RenderCopy();
			V4->RenderCopy();
			SDL_RenderPresent(sdlRenderer);
		}
		else if (event.type == SDL_WINDOWEVENT) {

			//动态掉整位置
			SDL_GetWindowSize(screen, &width, &height);
			V1->SetPosition(0, 0, int(width * 0.5), int(height * 0.5));
			V2->SetPosition(int(width * 0.5), 0, int(width * 0.5), int(height * 0.5));
			V3->SetPosition(0, int(height * 0.5), int(width * 0.5), int(height * 0.5));
			V4->SetPosition(int(width * 0.5), int(height * 0.5), int(width * 0.5), int(height * 0.5));
		}
		
		else if (event.type == SDL_KEYDOWN) {
			//Pause
			/*if (event.key.keysym.sym == SDLK_SPACE) {
				thread_pause = !thread_pause;
			}*/
				
		}
		else if (event.type == SDL_QUIT) {
			thread_exit = 1;
			return 0;
		}
		else if (event.type == SFM_BREAK_EVENT) {
			break;
		}
		//SDL_Delay(40);// 延迟40毫秒
	}


	SDL_Quit();
	return 0;
}